<?php
class uploadService extends commonService{
	private $upload_config=null;
    private $table=null;
	
    public function __construct(){
		parent::__construct();
		$this->table=table('uplog');
		$this->upload_config=$this->conf->ADMIN_UPLOAD;
		//$this->svc=load_service('uplog');
	}
    
    public function t(){
        return array('uplog');
    }
	//上传日志
    private function uplog($data){
        if(!is_array($data)) return false;
        $data['uniq']=md5(time().rand(100000,999999));
        $data['add_time']=time();
        $query="insert into `{$this->table}` 
        (`uniq`,`fileurl`,`filesize`,`fileext`,`kind`,`add_time`) 
        values (:uniq,:fileurl,:filesize,:fileext,:kind,:add_time)";
		$this->m->query($query,array('uniq'=>$data['uniq'],'fileurl'=>$data['fileurl'],'filesize'=>$data['filesize'],'fileext'=>$data['fileext'],'kind'=>$data['kind'],'add_time'=>$data['add_time']));
		return intval($this->m->insert_id());
    }
	//html5上传
	public function html5_($arr){
		$up=new html5Uploader('part',$this->upload_config['file'],false,$arr);
		$info = $up->getFileInfo();
		return $info;
	}
	//图片上传
    public function add_(){
		
        $up=new Uploader("upfile",$this->upload_config['image']);
        $info = $up->getFileInfo();
        if($info['state']=='SUCCESS'){
            $insert_id=$this->uplog(array('kind'=>'image','fileurl'=>$info['url'],'filesize'=>$info['size'],'fileext'=>$info['type']));
			$info['insert_id']=$insert_id;
		}
		$info['url_base']=$info['url'];
        $info['url']=$this->upload_config['image']['httpPath'].$info['url'];
		$info['kind']='image';
		return $info;
    }
	
	//附件上传
    public function add2_(){
		
        $up=new Uploader("upfile",$this->upload_config['file']);
        $info = $up->getFileInfo();
        if($info['state']=='SUCCESS'){
            $insert_id=$this->uplog(array('kind'=>'file','fileurl'=>$info['url'],'filesize'=>$info['size'],'fileext'=>$info['type']));
			$info['insert_id']=$insert_id;
		}
		$info['url_base']=$info['url'];
        $info['url']=$this->upload_config['file']['httpPath'].$info['url'];
		$info['kind']='file';
		return $info;
    }
	
	//缩略图重新修改文件信息
	public function update_log_info($data,$id){
		return $this->model->_u($this->t[0],$data,$id);
	}
	
	//删除
    public function del($id){
		$query="select `fileurl`,`kind` from `{$this->t[0]}` where `id`=:id limit 1";
		$r=$this->m->select($query,array('id'=>$id));
		if($r){
			unlink($this->disk_path($r['fileurl'],$r['kind']));
			$query="delete from `{$this->t[0]}` where `id`=:id limit 1";
			return $this->m->query($query,array('id'=>$id));
		}
		return true;
    }
    public function mylist($condition=array()){
        $return=array();
        $query="select * from `{$this->t[0]}` where 1 order by `id` desc";
        $p=new smart_pager(array($query,array()),$this->m);
        //$p->address('&block_id=%block_id%','block_id',$condition['block_id']);
        if($p->total_num>0){
            $return[]=$this->m->mselect($p->query);
            $return[]=$p;
        }
        return $return;
    }
	public function http_path($p,$kind){
		//$kind image file
		if(!array_key_exists($kind,$this->conf->ADMIN_UPLOAD)) exit('kind error!');
		$cfg=$this->conf->ADMIN_UPLOAD[$kind];
		return $cfg['httpPath'].$p;
	}
	public function disk_path($p,$kind){
		//$kind image file
		if(!array_key_exists($kind,$this->conf->ADMIN_UPLOAD)) exit('kind error!');
		$cfg=$this->conf->ADMIN_UPLOAD[$kind];
		return $cfg['savePath'].$p;
	}
	//转换附件大小
	function changeFileSize($filesize){ 
		if($filesize >= 1073741824){ 
			$filesize = round($filesize / 1073741824  ,2) . ' Gb'; 
		}else if($filesize >= 1048576) { 
			$filesize = round($filesize / 1048576 ,2) . ' Mb'; 
		}else if($filesize >= 1024) { 
			$filesize = round($filesize / 1024, 2) . ' Kb'; 
		}else{ 
			$filesize = $filesize . ' Bytes'; 
		} 
		return $filesize; 
	}
//
}